package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.SignSummaryOriginal;

/**
 * 创建者: HuXiaoDan
 * 日期: 2016/12/14 17:31
 */
public class SignSummaryDetailFragment extends Fragment {
  protected static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.SignSummaryDetailFragment.EXTRA_VALUE";
  private SignSummaryOriginal.SignSummaryContent mContent;

  private TextView tvStaffName;
  private TextView tvSignInTime;
  private TextView tvSignInPlace;
  private TextView tvSignIOutTime;
  private TextView tvSignIOutPlace;
  private TextView tvWorkingHours;
  private TextView tvAttendanceStatus;
  private TextView tvAttendanceList;
  private TextView tvRegStaffName;
  private TextView tvTvRegStaffDate;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.signSummaryDetail);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mContent = (SignSummaryOriginal.SignSummaryContent) getArguments().getSerializable(EXTRA_VALUE);
    super.onCreate(savedInstanceState);
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_sign_summarey, container, false);
    findViews(view);
    initData();
    return view;
  }

  private void findViews(View view) {
    tvStaffName = (TextView) view.findViewById(R.id.tv_staffName);
    tvSignInTime = (TextView) view.findViewById(R.id.tv_signInTime);
    tvSignInPlace = (TextView) view.findViewById(R.id.tv_signInPlace);
    tvSignIOutTime = (TextView) view.findViewById(R.id.tv_signIOutTime);
    tvSignIOutPlace = (TextView) view.findViewById(R.id.tv_signIOutPlace);
    tvWorkingHours = (TextView) view.findViewById(R.id.tv_workingHours);
    tvAttendanceStatus = (TextView) view.findViewById(R.id.tv_attendanceStatus);
    tvAttendanceList = (TextView) view.findViewById(R.id.tv_attendanceList);
    tvRegStaffName = (TextView) view.findViewById(R.id.tv_regStaffName);
    tvTvRegStaffDate = (TextView) view.findViewById(R.id.tv_tv_regStaffDate);
  }

  private void initData() {
    tvStaffName.setText(mContent.getStaffName());
    tvSignInTime.setText(mContent.getPsignInTime());
    tvSignInPlace.setText(mContent.getPsignInPlace());
    tvSignIOutTime.setText(mContent.getPsignOutTime());
    tvSignIOutPlace.setText(mContent.getPsignOutPlace());
    tvWorkingHours.setText(mContent.getOnPositionHours());
    //考勤状态
    tvAttendanceStatus.setText(mContent.getAttendStatusName());

    tvRegStaffName.setText(mContent.getRegStaffName());
    tvTvRegStaffDate.setText(mContent.getRegDate());
    if (!TextUtils.isEmpty(mContent.getRelRecKinds()) && "T".equalsIgnoreCase(
        mContent.getRelRecKinds())) {
      tvAttendanceList.setVisibility(View.VISIBLE);
      tvAttendanceList.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
          /**
           * 原来单据明细跳转到考勤申请列表,更改为跳转到考勤申请单汇总查询列表
           * 原因:批量加班单很多子表没法查询出其中一条
           */
          //                    CustomerDialog extraContent = new CustomerDialog(mContent.getRecordDate(), mContent.getStaffId());
          //                    Intent intent = new Intent(getActivity(), RequisitionListActivity.class);
          //                    intent.putExtra(CheckListFragment.EXTRA_VALUE, extraContent);
          //                    startActivity(intent);

          CustomerDialog extraContent =
              new CustomerDialog(mContent.getRegStaffName(), mContent.getRecordDate());
          Intent intent = new Intent(getActivity(), AttendanceSummaryQueryListActivity.class);
          intent.putExtra(AttendanceSummaryQueryListFragment.EXTRA_VALUE, extraContent);
          startActivity(intent);
        }
      });
    } else {
      tvAttendanceList.setVisibility(View.INVISIBLE);
    }
  }

  public static SignSummaryDetailFragment newInstance(
      SignSummaryOriginal.SignSummaryContent content) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_VALUE, content);
    SignSummaryDetailFragment fragment = new SignSummaryDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
